package com.huika.renrench.control.main.shoptf.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.encrypt.StringUtils;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.lib.utils.StringTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.activity.LoginActivity;
import com.huika.renrench.control.main.shoptf.gallery.ImageDetailAct;
import com.huika.renrench.control.main.shoptf.gallery.ProductImageArray;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.LocationBean;
import com.huika.renrench.support.bean.LocationBean.MyLocationListener;
import com.huika.renrench.support.bean.ShopTransferListBean;
import com.huika.renrench.ui.GaodeMapRouteActivity;
import com.huika.renrench.utils.GaodeMapHelper;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.view.TitleBarHelper;

/**
 * @author chenyuan
 * @date：2015年6月2,下午2:15:30
 * @describe:店铺转让详情activity
 */
public class ShopTransferDetailActivity extends BaseAct implements
		Listener<RequestResult<ShopTransferListBean>>, MyLocationListener {
	public static final String PROJECT_TAG = "project";
	/** 图片 */
	private ImageView img_iv;
	/** 项目名称 */
	private TextView tv_name;
	/** 项目价格 */
	private TextView tv_price;
	/** 面积 */
	private TextView tv_areass;
	/** 临近 */
	private TextView tv_desc;
	/** 区域 */
	private TextView tv_area;
	/** 区域 */
	private TextView tv_transfer_fee;
	/** 简介 */
	private TextView tv_introduction;
	/** 商家地址 */
	private TextView tv_owner_address;
	/** 点击查看相册 */
	private LinearLayout click_to_see_albumLoy;
	private ShopTransferListBean projectList = new ShopTransferListBean();
	private ShopTransferListBean project = new ShopTransferListBean();
	private TitleBarHelper titleBarHelper;

	private String shopId;

	private LocationBean locationBean;
	private GaodeMapHelper gaodeMapHelper;

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		super.initData();
		projectList = (ShopTransferListBean) getIntent().getSerializableExtra(
				PROJECT_TAG);

		if (projectList == null) {
			Intent intent = getIntent();
			shopId = (String) intent.getExtras().get("shopId");
		} else {
			shopId = projectList.shopId + "";
		}

		locationBean = GlobalApp.getInstance().locationBean;
		if (GlobalApp.getInstance().isLocated()) {
			getNetData();
		} else {
			startLocation();
		}

	}

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_shop_transfer_detail);
	}

	private void startLocation() {
		showLoadingDialog("定位中");
		gaodeMapHelper = new GaodeMapHelper(this);
		gaodeMapHelper.setmLocationListener(this);
		gaodeMapHelper.start();
	}

	@Override
	public void onStop() {
		super.onStop();
		if (gaodeMapHelper != null) {
			gaodeMapHelper.stop();
		}
	}

	@Override
	protected void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();

		titleBarHelper = new TitleBarHelper(this, -1, R.string.about);

		titleBarHelper.setOnRightClickListener(this);

		initTopView();

		img_iv = (ImageView) findViewById(R.id.img_iv);

		tv_name = (TextView) findViewById(R.id.tv_name);

		tv_price = (TextView) findViewById(R.id.tv_price);
		// tv_price.setText(MoneyShowTool.nolastDF(project.getPrice()));

		tv_areass = (TextView) findViewById(R.id.tv_long);
		tv_desc = (TextView) findViewById(R.id.tv_desc);
		tv_area = (TextView) findViewById(R.id.tv_area);
		tv_transfer_fee = (TextView) findViewById(R.id.tv_transfer_fee);
		tv_introduction = (TextView) findViewById(R.id.tv_introduction);
		// tv_owner_name = (TextView) findViewById(R.id.tv_owner_name);
		tv_owner_address = (TextView) findViewById(R.id.tv_owner_address);
		click_to_see_albumLoy = (LinearLayout) findViewById(R.id.click_to_see_albumLoy);
		click_to_see_albumLoy.setOnClickListener(this);

		// tv_owner_name.setOnClickListener(this);
		tv_owner_address.setOnClickListener(this);
		findViewById(R.id.icon_tell).setOnClickListener(this);

		/** 获取网络数据 **/
		getNetData();

	}

	private void getUserId(JSONWrapAjaxParams ajaxParams) {
		String u_id = GlobalApp.getInstance().getUserId();
		if (!TextUtils.isEmpty(u_id)) {
			ajaxParams.putCommonTypeParam("userId", u_id);
		} else {
			ajaxParams.putCommonTypeParam("userId", "0");
		}
	}

	private void getNetData() {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("shopId", shopId);
		if (GlobalApp.getInstance().isLocated()) {
			ajaxParams.putCommonTypeParam("latitude", locationBean.latitude);
			ajaxParams.putCommonTypeParam("longitude", locationBean.longtitude);
		}
		getUserId(ajaxParams);
		FormResultRequest<ShopTransferListBean> getResultRequest = new FormResultRequest<ShopTransferListBean>(
				UrlConstants.GETSHOPSTOREDETAIL, this, this,
				new TypeToken<RequestResult<ShopTransferListBean>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		executeRequest(getResultRequest);

	}

	private void initTopView() {
		RelativeLayout rl_top_imgs = (RelativeLayout) findViewById(R.id.rl_top_imgs);
		LayoutParams layoutParams = (LayoutParams) rl_top_imgs
				.getLayoutParams();
		layoutParams.height = GlobalApp.getInstance().SCREEN_WIDTH * 390 / 640;
		rl_top_imgs.setLayoutParams(layoutParams);
	}

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		super.widgetClick(v);

		switch (v.getId()) {
		case R.id.icon_tell:// 拨号
			if (null != project) {
				if (!TextUtils.isEmpty(project.phone)) {
					Intent telIntent = new Intent(Intent.ACTION_DIAL);
					telIntent.setData(Uri.parse("tel:" + project.phone));
					startActivity(telIntent);
				}
				// String tell = TextUtils.isEmpty(project.phone) ?
				// getString(R.string.about_tell)
				// : project.phone;

			}

			break;
		case R.id.click_to_see_albumLoy:// 相册
			showGallery();
			break;
		case R.id.tv_owner_name:// 地图路径
		case R.id.tv_owner_address:
			Intent goalIntent = new Intent(this, GaodeMapRouteActivity.class);
			goalIntent.putExtra("project", project);
			startActivity(goalIntent);
			break;
		case R.id.right:
			if (!LoginUtils.isLogin()) {
				showActivity(this, LoginActivity.class);
				return;
			}
			getAddCollect();
			break;
		}

	}

	private void getAddCollect() {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("collectId", projectList.shopId);
		ajaxParams.putCommonTypeParam("type", "2");
		getUserId(ajaxParams);
		FormResultRequest<HashMap<String, String>> getResultRequest = new FormResultRequest<HashMap<String, String>>(
				UrlConstants.ADDCOLLECT, getAddCollectRsp, this,
				new TypeToken<RequestResult<HashMap<String, String>>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		executeRequest(getResultRequest);

	}

	private Listener<RequestResult<HashMap<String, String>>> getAddCollectRsp = new Listener<RequestResult<HashMap<String, String>>>() {

		@SuppressLint("ResourceAsColor")
		@Override
		public void onResponse(RequestResult<HashMap<String, String>> result) {
			if (result != null && result.flag == 1) {
				// setAreaClassifyData(result.getRs());
				// showToastMsg(result.msg);
				showToastMsg("收藏成功！");
				titleBarHelper.setRightMsg("已收藏");
				titleBarHelper.setRightMsgColor(getResources().getColor(
						R.color.color_969696));
				titleBarHelper.setOnRightClickListener(null);
			} else {
			}

			dismissLoadingDialog();
		}
	};

	protected List<ProductImageArray> imgUrls;
	private String[] imgs = {
			"http://img0.imgtn.bdimg.com/it/u=3766230679,95318279&fm=21&gp=0.jpg",
			"http://img1.imgtn.bdimg.com/it/u=2984036353,594873942&fm=21&gp=0.jpg",
			"http://img1.imgtn.bdimg.com/it/u=3594953304,3934544117&fm=21&gp=0.jpg" };

	private void showGallery() {
		imgUrls = new ArrayList<ProductImageArray>();
		// for (int i = 0; i < imgs.length; i++) {
		// ProductImageArray bean = new ProductImageArray();
		// bean.setImageUrl(imgs[i]);
		// bean.setBigImageUrl(imgs[i]);
		// imgUrls.add(bean);
		// }

		if (!project.imageArray.isEmpty() && project.imageArray.size() > 0) {
			imgUrls.addAll(project.imageArray);

			Bundle bundle = new Bundle();
			bundle.putParcelableArrayList("imageListBean",
					(ArrayList<? extends Parcelable>) imgUrls);
			bundle.putString("imageDetailCheck", imgUrls.get(0).getImageUrl());
			Intent intent = new Intent(this, ImageDetailAct.class);
			intent.putExtras(bundle);
			startActivity(intent);
		}
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void onResponse(RequestResult<ShopTransferListBean> result) {
		dismissLoadingDialog();
		if (result.flag == 1) {
			if (result.getRs() != null) {
				project = result.getRs();
				if (project.imageArray != null && project.imageArray.size() > 0) {
					GlobalApp.loadImgLoader(img_iv, project.imageArray.get(0)
							.getBigImageUrl());
				}
				tv_desc.setText("临近：" + project.nearby);
				tv_areass.setText("面积：" + project.getAcreage() + "㎡");
				if (TextUtils.isEmpty(project.areaName)) {
					tv_area.setText("区域：");
				} else {
					if (project.areaName.equals("null")) {
						tv_area.setText("区域：");
					} else {
						tv_area.setText("区域：" + project.areaName);
					}
				}

				if (TextUtils.isEmpty(project.introduction)) {
					tv_introduction.setText("简介：");
				} else {
					if (project.introduction.equals("null")) {
						tv_introduction.setText("简介：");
					} else {
						tv_introduction.setText("简介：" + project.introduction);
					}
				}

				tv_owner_address.setText(project.address);
				if(!project.price.equals("面议")) {
					
					if (project.getPrice().equals("null") || project.getPrice() == null
							|| project.getPrice().equals("0")) {
						tv_price.setText("￥面议");
					} else {
						tv_price.setText("￥" + com.huika.lib.utils.StringUtils.getWan(Double.parseDouble(project.price)));
					}
				} else {
					tv_price.setText("￥" + project.price);
				}

				titleBarHelper.setTitleMsg("店铺详情");
				tv_name.setText(project.shopName);
				if (!TextUtils.isEmpty(project.changeType)&&!project.changeType.equals("0")&&!project.changeType.equals("null")) {
					tv_transfer_fee.setText("￥" + project.changeType);
				}else{
					tv_transfer_fee.setText("￥面议");
				}
				

				if (project.isAttention) {
					titleBarHelper.setRightMsg("已收藏");
					titleBarHelper.setOnRightClickListener(null);
					titleBarHelper.setRightMsgColor(getResources().getColor(
							R.color.color_969696));
				} else {
					titleBarHelper.setRightMsg("收藏");
					titleBarHelper.setOnRightClickListener(this);
				}
			}
		}

	}

	@Override
	public void onErrorResponse(VolleyError error) {
		// TODO Auto-generated method stub
		super.onErrorResponse(error);
	}

	@Override
	public void onLocation(LocationBean locationBean) {
		// TODO Auto-generated method stub
		if (!TextUtils.isEmpty(locationBean.province)) {
			GlobalApp.getInstance().setLocated(true);
		} else {// 定位失败
			showToastMsg("定位失败");
			dismissLoadingDialog();
		}

		getNetData();
	}

}
